package com.jic.finance.controller;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.common.utils.IDUtils;
import com.jic.finance.common.util.BeanUtil;
import com.jic.finance.entity.PaymentMethod;
import com.jic.finance.request.manager.PaymentMethodRequest;
import com.jic.finance.response.manager.PaymentMethodResponse;
import com.jic.finance.service.PaymentMethodService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author: MJ
 * @date: 2020-01-09
 * @remark: 支付方式后台管理系统的服务
 */
@Slf4j
@RestController
@RequestMapping("/paymentMethod")
public class PaymentMethodController {


    @Autowired
    private RedisClientTemplate redisClientTemplate;

    @Autowired
    PaymentMethodService paymentMethodService;


    /**
     * 分页查询
     * @param form
     * @return
     */
    @ApiOperation(value = "获取支付方式设置")
    @ResponseBody
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public RestResult<PageResult<PaymentMethodResponse>> queryList(@RequestBody PaymentMethodRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        PaymentMethodRequest paymentMethodRequest = BeanUtil.copy(form, PaymentMethodRequest.class);
        PageResult<PaymentMethod> result = paymentMethodService.queryPage(paymentMethodRequest, page);
        List<PaymentMethodResponse> paymentMethodResponseList = result.getRows().stream().map(temp -> {
            PaymentMethodResponse paymentMethodResponse = BeanUtil.copy(temp, PaymentMethodResponse.class);
            return paymentMethodResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<PaymentMethodResponse> pageResult = new PageResult<>();
        pageResult.setRows(paymentMethodResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }



    /*
    * 新增/修改支付方式设置
    * */
    @ApiOperation(value = "新增/修改支付方式设置")
    @ResponseBody
    @RequestMapping("/editPaymentMethod")
    public RestResult<String> editPaymentMethod(@RequestBody PaymentMethodRequest form) {
        RestResult restResult = new RestResult<>();
        try {
            if (StringUtils.isEmpty(form.getToken())){
                throw new Exception("必传参数为空 token");
            }
            String userStr = redisClientTemplate.get(form.getToken());
            if (StringUtils.isEmpty(userStr)){
                return RestResult.error("4000","登录失效");
            }
            Map sysUser = JSONArray.parseObject(userStr, Map.class);
            //修改
            if(null!=form.getId()&&!"".equals (form.getId())){
                form.setUpdater(Long.valueOf(sysUser.get("id").toString()));
                form.setUpdaterName(sysUser.get("username").toString());
                long flag=paymentMethodService.update(form);
                restResult=RestResult.success(restResult);
            }else {
                form.setMethodCode(IDUtils.GenerateBillNo("PM"));
                form.setCreator(Long.valueOf(sysUser.get("id").toString()));
                form.setCreatorName(sysUser.get("username").toString());
                long flag=paymentMethodService.insert(form);
                restResult=RestResult.success(restResult);
            }

        } catch (Exception e) {
            e.printStackTrace();
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }


    /*
     * 删除支付方式设置
     * */
    @ApiOperation(value = "删除支付方式设置")
    @ResponseBody
    @RequestMapping("/deleteInfo")
    public RestResult<String> deleteInfo(@RequestBody PaymentMethodRequest form) {
        RestResult restResult = new RestResult<>();
        try {
            String[] checked = form.getIds().split(",");
            for(int i=0;i<checked.length;i++){
                paymentMethodService.delete(Long.valueOf(checked[i]));
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return RestResult.success("成功");
    }
}
